package api

import (
	"Im/dao"
	"Im/dto"
	"Im/global"
	"Im/model"
	"Im/service"
	"Im/utils"
	"errors"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

func UserLogin(c *gin.Context) {
	var loginDto dto.UserLoginDto
	err := c.ShouldBindJSON(&loginDto)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
			"code": "500",
			"msg":  err.Error(),
		})
		return
	}
	login, err := service.Login(loginDto)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
			"code": "400",
			"msg":  errors.New("账号或密码错误").Error(),
		})
		return
	}
	token, _ := utils.GenerateToken(login.Id, login.Email)
	c.AbortWithStatusJSON(http.StatusOK, gin.H{
		"code":  "200",
		"data":  login,
		"token": token,
	})
}

// @Tags User
// @Summary 获取用户详情
// @Description 用户详情的接口
// @Accept  json
// @Router /auth/user/details [get]
func UserDetail(c *gin.Context) {
	user_claims, _ := c.Get("user_claims")
	u := user_claims.(utils.JwtCastClaims)
	detail, err := service.UserDetail(u.ID)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
			"msg": err.Error(),
		})
		return
	}
	c.AbortWithStatusJSON(http.StatusOK, gin.H{
		"data": detail,
		"msg":  "请求成功",
	})
}

//获取用户的消息列表

func GetUserMsg(c *gin.Context) {
	var uMsgDto dto.UserMsgDto
	err := c.ShouldBindJSON(&uMsgDto)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
			"msg": err.Error(),
		})
		return
	}
	uc := c.MustGet("user_claims").(utils.JwtCastClaims)
	data, err := dao.GetUserMsg(uc.UserId, uMsgDto.PgSize, uMsgDto.PgNum)
	if err != nil {
		return
	}
	fmt.Println(data)
	c.AbortWithStatusJSON(http.StatusOK, data)

}

// 注册用户的函数
func SignUp(c *gin.Context) {
	var usDto dto.SignUpDto
	err := c.ShouldBindJSON(&usDto)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
			"msg": err.Error(),
		})
		return
	}
	//校验用户传来的验证码是否正确
	if global.EmailCode[usDto.Email] != usDto.EmailCode {
		c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
			"msg": "验证码错误",
		})
		return
	}
	err = dao.SignUp(usDto.Account, usDto.Password, usDto.NickName, usDto.Email)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
			"msg": err.Error(),
		})
		return
	}
	c.AbortWithStatusJSON(http.StatusOK, gin.H{
		"msg": "注册成功",
	})
	delete(global.EmailCode, usDto.Email)
}

//更新用户头像的函数

func UpdateUser(c *gin.Context) {
	user_claims, _ := c.Get("user_claims")
	u := user_claims.(utils.JwtCastClaims)
	var upUserDto dto.UpdateUserDto
	var stFilePath string
	err := c.ShouldBind(&upUserDto)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
			"msg": "参数错误" + err.Error(),
		})
		return
	}
	file, err := c.FormFile("file")
	file2, _ := file.Open()
	utils.ImgCompress(file2, file.Filename)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
			"msg": "图片上传失败",
		})
		return
	}
	if file != nil {
		stFilePath = fmt.Sprintf("./static/img/%s", file.Filename)
	}

	var user model.UserBasic

	user.Avatar = stFilePath
	user.Id = u.ID
	user.Nickname = upUserDto.NickName
	err = dao.UpdateUser(user)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
			"msg": "更新失败" + err.Error(),
		})
		return
	}
	c.AbortWithStatusJSON(http.StatusOK, gin.H{
		"msg": "更新成功",
		"data": gin.H{
			"avatar":   stFilePath,
			"nickName": upUserDto.NickName,
		},
	})
}

// 添加好友申请的api
func AddFriend(c *gin.Context) {
	userClaims, _ := c.Get("user_claims")
	u := userClaims.(utils.JwtCastClaims)
	//首先要用dto对前端传过来的参数进行验证
	var addFriendDto dto.AddFriendsDto
	err := c.ShouldBindJSON(&addFriendDto)
	addFriendDto.ID = u.ID
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
			"msg": "参数错误" + err.Error(),
		})
		return
	}
	//验证完前端传过来过来的参数，开始添加好友申请
	service.AddFriends(addFriendDto)
}

// 删除好友申请以及判断是否添加好友的api
func DeleteApplication(c *gin.Context) {
	userClaims, _ := c.Get("user_claims")
	u := userClaims.(utils.JwtCastClaims)

	var deleteApplicationDto dto.AddFriendsDto
	err := c.ShouldBindJSON(&deleteApplicationDto)
	deleteApplicationDto.ID = u.ID
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
			"msg": "参数错误" + err.Error(),
		})
		return
	}
	//
	err = service.DeleteApplication(deleteApplicationDto)
	if err != nil {
		return
	}
}
